package com.xnyzc.lhy.order.service.impl.task;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xnyzc.lhy.common.component.redis.RedisCommon;
import com.xnyzc.lhy.common.constant.RedisPrefixConstant;
import com.xnyzc.lhy.common.entity.Qw;
import com.xnyzc.lhy.common.penum.EOrderType;
import com.xnyzc.lhy.common.penum.order.EOrderStatus;
import com.xnyzc.lhy.common.penum.vehicle.ECallVehType;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.DateUtil;
import com.xnyzc.lhy.common.util.IDUtil;
import com.xnyzc.lhy.order.entity.order.OaCOrderPersonal;
import com.xnyzc.lhy.order.entity.user.OaDUserInfo;
import com.xnyzc.lhy.order.entity.work.OaDWorkRecord;
import com.xnyzc.lhy.order.mapper.order.OaCOrderPersonalMapper;
import com.xnyzc.lhy.order.mapper.user.OaDUserInfoMapper;
import com.xnyzc.lhy.order.mapper.work.OaDWorkRecordMapper;
import com.xnyzc.lhy.order.service.task.IBatchUpdateOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @Description 批量更新接单数据
 * @Author zhuyubin
 * @Date 2019-09-02 15:18
 **/
@Service
public class BatchUpdateOrdersServiceImpl implements IBatchUpdateOrdersService {

    @Autowired
    private OaDWorkRecordMapper oaDWorkRecordMapper;

    @Autowired
    private RedisCommon redisCommon;

    @Autowired
    private OaDUserInfoMapper oaDUserInfoMapper;

    @Autowired
    private OaCOrderPersonalMapper oaCOrderPersonalMapper;

    /**
     * 批量更新跨天接单数据
     */
    @Override
    public void updateOrdersData() {
        // 当前时间
        Date date = new Date();
        Date dateStart = new Date(DateUtil.getDayStartTimeNew(date).getTime());
        Date dateEnd = new Date(DateUtil.getDayEndTimeNew(date).getTime());
        // 昨天时间
        Date yesterday = DateUtil.dateAddDays(date, -1);
        Date yesterdayStart = new Date(DateUtil.getDayStartTimeNew(yesterday).getTime());
        Date yesterdayEnd = new Date(DateUtil.getDayEndTimeNew(yesterday).getTime());
        // 跨天完整的接单数据(有开始、有结束)
        List<OaDWorkRecord> oaDWorkRecords = oaDWorkRecordMapper.selectList(Wrappers.<OaDWorkRecord>lambdaQuery()
                .ge(OaDWorkRecord::getCreateTime, yesterdayStart)
                .le(OaDWorkRecord::getCreateTime, dateEnd)
                .isNotNull(OaDWorkRecord::getEndTime)
        );
        for (OaDWorkRecord oaDWorkRecord : oaDWorkRecords) {
            Date endTime = oaDWorkRecord.getEndTime();
            Date startTime = oaDWorkRecord.getStartTime();
            if (CheckUtil.objIsNotEmpty(endTime)) {
                if (endTime.getTime() >= dateStart.getTime() && startTime.getTime() < dateStart.getTime()) {
                    // 将跨天订单拆分成两个
                    // 结束昨天的接单
                    oaDWorkRecord.setUpdateTime(date);
                    oaDWorkRecord.setEndTime(yesterdayEnd);
                    oaDWorkRecordMapper.updateById(oaDWorkRecord);
                    // 创建当天新的接单
                    oaDWorkRecord.setRecordId(IDUtil.nextId());
                    oaDWorkRecord.setStartTime(dateStart);
                    oaDWorkRecord.setEndTime(endTime);
                    oaDWorkRecord.setVersion(0L);
                    oaDWorkRecord.setCreateTime(date);
                    oaDWorkRecordMapper.insert(oaDWorkRecord);
                }
            }
        }
        // 跨天不完整的接单数据(有开始、无结束)
        oaDWorkRecords = oaDWorkRecordMapper.selectList(Wrappers.<OaDWorkRecord>lambdaQuery()
                .ge(OaDWorkRecord::getCreateTime, yesterdayStart)
                .le(OaDWorkRecord::getCreateTime, dateEnd)
                .isNull(OaDWorkRecord::getEndTime)
                .orderByDesc(OaDWorkRecord::getDriverId)
                .orderByDesc(OaDWorkRecord::getCreateTime)
        );
        if (CheckUtil.objIsNotEmpty(oaDWorkRecords)) {
            // 默认司机id
            Long driverId = -1L;
            for (OaDWorkRecord oaDWorkRecord : oaDWorkRecords) {
                Date startTime = oaDWorkRecord.getStartTime();
                if (CheckUtil.objIsEmpty(startTime)) {
                    continue;
                }
                // 当前司机id
                Long id = oaDWorkRecord.getDriverId();
                if (CheckUtil.objIsEmpty(id)) {
                    continue;
                }
                if (dateStart.getTime() <= startTime.getTime()) {
                    continue;
                }
                if (!driverId.equals(id)) {
                    // 结束昨天的接单
                    oaDWorkRecord.setEndTime(yesterdayEnd);
                    oaDWorkRecord.setUpdateTime(date);
                    oaDWorkRecordMapper.updateById(oaDWorkRecord);
                    // 创建当天新的接单
                    oaDWorkRecord.setRecordId(IDUtil.nextId());
                    oaDWorkRecord.setStartTime(dateStart);
                    oaDWorkRecord.setEndTime(null);
                    oaDWorkRecord.setVersion(0L);
                    oaDWorkRecord.setCreateTime(date);
                    oaDWorkRecordMapper.insert(oaDWorkRecord);
                    driverId = id;
                }
            }
        }
    }

    /**
     * 活跃监听
     */
    @Override
    public void driverActiveListen() {
        //获取所以未结束的订单单
        List<OaDWorkRecord> oaDWorkRecords = oaDWorkRecordMapper.selectList(Wrappers.<OaDWorkRecord>lambdaQuery()
                .isNull(OaDWorkRecord::getEndTime)
                .orderByDesc(OaDWorkRecord::getDriverId)
                .orderByDesc(OaDWorkRecord::getCreateTime)
        );
        for (OaDWorkRecord oaDWorkRecord : oaDWorkRecords) {
            //判断是否活跃,如果活跃退出循环
            Long driverId = oaDWorkRecord.getDriverId();
            if (redisCommon.hashKey(RedisPrefixConstant.GPS_VEH_KEY_PREFIX + driverId)) {
                continue;
            }
            //获取司机信息
            OaDUserInfo oaDUserInfo = oaDUserInfoMapper.selectOne(Qw.create().eq(OaDUserInfo.DRIVER_ID, driverId));
            if (CheckUtil.objIsNotEmpty(oaDUserInfo)) {
                //在未接单的情况下,关闭当前接单状态
                if (oaDUserInfo.getIsOrder() == 0) {
                    // 结束接单
                    Date date = new Date();
                    oaDWorkRecord.setEndTime(date);
                    oaDWorkRecord.setUpdateTime(date);
                    oaDWorkRecordMapper.updateById(oaDWorkRecord);
                    //关闭接单状态
                    oaDUserInfo.setAcceptancOrderStatus(0);
                    oaDUserInfoMapper.updateById(oaDUserInfo);
                }
            }
        }
    }

    /**
     * 判断订单是否超时
     */
    @Override
    public void updateOrderStatus() {
        List<OaCOrderPersonal> list = oaCOrderPersonalMapper.selectList(Qw.create().eq(OaCOrderPersonal.ORDER_STATUS, 0));
        //获取所有订单状态0的
        if (list.size() > 0) {
            for (OaCOrderPersonal oaCOrderPersonal : list) {
                Date createTime = oaCOrderPersonal.getCreateTime();
                if (CheckUtil.objIsEmpty(createTime)) {
                    continue;
                }
                Long aLong = DateUtil.dateSubtractLong(createTime, new Date());
                if (aLong > 20 && oaCOrderPersonal.getOrderType() == ECallVehType.expressTrain.getType()) {
                    //实时订单才超时
                    oaCOrderPersonal.setOrderStatus(EOrderStatus.orderTimeout.getType());
                    oaCOrderPersonal.setUpdateTime(new Date());
                    oaCOrderPersonalMapper.updateById(oaCOrderPersonal);
                }
            }
        }
    }
}
